/*
 * MadHelix is a Java Swing-based GUI frontend for SoundHelix.
 * Copyright (C) 2017-2019, 2023 UltrasonicMadness
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ultrasonicmadness.madhelix.dialogs;

import org.ultrasonicmadness.madhelix.utils.VersionUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

public class AboutBox extends JDialog {
    private JPanel infoPanel = new JPanel();
    private JTabbedPane mainPane = new JTabbedPane();
    private JPanel creditsPanel = new JPanel();
    private JPanel licensePanel = new JPanel();

    public AboutBox(JFrame mainWindow) {
        super(mainWindow, false);
        initAboutBox();
    }

    private void initAboutBox() {
        // Declare and initialize objects
        this.setTitle("About");
        JPanel mainPanel = new JPanel();

        // Setup
        setUpInfoPanel();
        setUpMainPane();

        // Layouts
        mainPanel.setLayout(new GridBagLayout());

        // Set up constraints
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(4, 4, 4, 4);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;

        // Add widgets to about box
        constraints.gridy = 0;
        constraints.weighty = 0;
        mainPanel.add(infoPanel, constraints);

        constraints.gridy = 1;
        constraints.weighty = 1;
        mainPanel.add(mainPane, constraints);

        // Set up the main window
        this.add(mainPanel);
        this.setMinimumSize(new Dimension(260, 260));
        this.setSize(540, 405);
        this.setLocationRelativeTo(null);
    }

    private void setUpInfoPanel() {
        infoPanel.setLayout(new GridBagLayout());

        // Set up labels
        JLabel headerLabel = new JLabel("MadHelix " +
                VersionUtils.getMadHelixVersionAsString());
        JTextArea versionNameLabel = new JTextArea('"' + VersionUtils.VERSION_NAME + '"');
        JTextArea descriptionLabel = new JTextArea("A Java Swing frontend for " +
                "the SoundHelix music generation tool.");
        JTextArea helixVersionLabel = new JTextArea();

        // In case an error occurs or SoundHelix.jar cannot be found
        try {
            helixVersionLabel.setText("Using SoundHelix version: " +
                    VersionUtils.getSoundHelixVersionAsString());
        } catch (Exception e) {
            helixVersionLabel.setText(e.getMessage().toString());
        } catch (NoClassDefFoundError e) {
            helixVersionLabel.setText("Cannot find module " + e.getMessage());
        }

        versionNameLabel.setEditable(false);
        versionNameLabel.setOpaque(false);
        descriptionLabel.setEditable(false);
        descriptionLabel.setOpaque(false);
        descriptionLabel.setLineWrap(true);
        descriptionLabel.setWrapStyleWord(true);
        helixVersionLabel.setEditable(false);
        helixVersionLabel.setOpaque(false);
        helixVersionLabel.setLineWrap(true);
        helixVersionLabel.setWrapStyleWord(true);

        // Display the application's name as a header with a larger font
        headerLabel.setFont(new Font("sans-serif", Font.BOLD, 24));

        // Set up constraints
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(4, 4, 4, 4);

        // Add objects to information panel
        constraints.gridy = 0;
        infoPanel.add(headerLabel, constraints);

        constraints.gridy = 1;
        infoPanel.add(versionNameLabel, constraints);

        constraints.gridy = 2;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.weightx = 1;
        infoPanel.add(descriptionLabel, constraints);

        constraints.gridy = 3;
        infoPanel.add(helixVersionLabel, constraints);
    }

    private void setUpMainPane() {
        setUpCreditsPanel();
        setUpLicensePanel();

        // Add panels to pane as tabs.
        mainPane.add("Credits", creditsPanel);
        mainPane.add("License", licensePanel);
    }

    private void setUpCreditsPanel() {
        // Set up text area
        JTextArea textArea = new JTextArea();

        creditsPanel.setLayout(new BorderLayout());

        // Text area configuration
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setText(
            "MadHelix developer\n" +
            "UltrasonicMadness\n" +
            "<contact@ultrasonicmadness.org>\n\n" +
            "SoundHelix developer\n" +
            "Thomas Sch\u00FCrger\n" +
            "<thomas@schuerger.com>\n\n" +
            "Icons\n" +
            "GNOME Project\n" +
            "<http://www.gnome.org/>"
        );

        // Add objects to main frame
        creditsPanel.add(new JScrollPane(textArea));
    }

    private void setUpLicensePanel() {
        // Set up text area
        JTextArea textArea = new JTextArea();

        licensePanel.setLayout(new BorderLayout());

        // Text area configuration
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setText(
            "This program is free software: you can redistribute it and/or modify " +
            "it under the terms of the GNU General Public License version 3 only, " +
            "as published by the Free Software Foundation.\n\n" +

            "This program is distributed in the hope that it will be useful, " +
            "but WITHOUT ANY WARRANTY; without even the implied warranty of " +
            "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the " +
            "GNU General Public License for more details.\n\n" +

            "You should have received a copy of the GNU General Public License " +
            "along with this program.  If not, see <http://www.gnu.org/licenses/>."
        );

        // Add objects to main frame
        licensePanel.add(new JScrollPane(textArea));
    }
}
